package NWish.properties.materials
{
	import NWish.Logger;
	import NWish.json.OrderDict;
	import NWish.properties.Properties;
	
	import away3d.materials.BitmapMaterial;
	
	class TextureProperties
	{
		private var _parent;
		private var _textureName;
		
		public function TextureProperties(parent,name)
		{
			_parent = parent;
			_textureName = name;
		}
		
		public function get fileSystem()
		{
			if(_parent)
			{
				return _parent.getPlugin('FileSystem');
			}
		}
		
		public function propertiesToDict(mat,dict:OrderDict,read_only,saving = false)
		{
			if(read_only == false)
			{
				if(mat.extra && mat.extra[_textureName])
					dict.push(_textureName,mat.extra[_textureName]);
				else
					dict.push(_textureName,'');
				
				if(saving == false)
				{
					var textures = fileSystem.getFileNames('.jpg','.png');
					dict.push(Properties.readMark + 'textuers ' + _textureName, textures);
				}
			}
			else
			{
			}
		}
		
		public function dictToProperties(dict,mat,scene,isloading)
		{
			var tex = dict[_textureName];
			
			if(tex == null)
			{
			}
			else if (tex == '')
			{
				mat[_textureName] = null;
				
				if(mat.extra != null)
					mat.extra[_textureName] = '';
			}
			else if(_parent)
			{
				var dm = fileSystem.getResource(tex);
				
				if(dm)
				{
					mat[_textureName] = dm;
					
					if(mat.extra == null)
						mat.extra = new Object();
					
					mat.extra[_textureName] = tex;
					return;
				}
			}
		}
		
	}

}